import React from "react";
import { CellProps, Column } from "react-table";

import { IVulnerabilitySoftware } from "interfaces/software";
import { IStringCellProps } from "interfaces/datatable_config";

import PATHS from "router/paths";

import LinkCell from "components/TableContainer/DataTable/LinkCell";
import TextCell from "components/TableContainer/DataTable/TextCell";
import HeaderCell from "components/TableContainer/DataTable/HeaderCell";
import ViewAllHostsLink from "components/ViewAllHostsLink";
import SoftwareIcon from "pages/SoftwarePage/components/icons/SoftwareIcon";
import { InjectedRouter } from "react-router";
import { getPathWithQueryParams } from "utilities/url";

type SwVulnTableColumnConfig = Column<IVulnerabilitySoftware>;

type ITableStringCellProps = IStringCellProps<IVulnerabilitySoftware>;
type IHostCountCellProps = CellProps<
  IVulnerabilitySoftware,
  number | undefined
>;
type IViewAllHostsLinkProps = CellProps<IVulnerabilitySoftware>;

const generateColumnConfigs = (
  isPremiumTier: boolean,
  router: InjectedRouter,
  teamIdForApi?: number
): SwVulnTableColumnConfig[] => {
  const configs: SwVulnTableColumnConfig[] = [
    {
      Header: "Name",
      disableSortBy: true,
      accessor: "name",
      Cell: ({ row }: ITableStringCellProps) => {
        const { name, id } = row.original;
        // since No Teams not supported on this page, falsiness of 0 is okay
        const path = getPathWithQueryParams(
          PATHS.SOFTWARE_VERSION_DETAILS(id.toString()),
          { team_id: teamIdForApi }
        );

        // Currently does not support icon_url
        // Cleanup: Refactor to use SoftwareNameCell when icon_url is available
        return (
          <LinkCell
            path={path}
            tooltipTruncate
            prefix={<SoftwareIcon name={name} />}
            value={name}
          />
        );
      },
    },
    {
      Header: "Version",
      disableSortBy: true,
      accessor: "version",
      Cell: ({ cell }: ITableStringCellProps) => (
        <TextCell value={cell.value} />
      ),
    },
    {
      Header: () => (
        <HeaderCell
          value={
            <>
              Resolved in <div className="resolved-suffix">version</div>
            </>
          }
          disableSortBy
        />
      ),
      disableSortBy: true,
      accessor: "resolved_in_version",
      Cell: ({ cell }: ITableStringCellProps) => (
        <TextCell value={cell.value} />
      ),
    },
    {
      Header: "Hosts",
      disableSortBy: true,
      accessor: "hosts_count",
      Cell: ({ row }: IHostCountCellProps) => {
        const { hosts_count } = row.original;
        return <TextCell value={hosts_count} />;
      },
    },
    {
      Header: "",
      id: "view-all-hosts",
      disableSortBy: true,
      Cell: (cellProps: IViewAllHostsLinkProps) => {
        return (
          <>
            {cellProps.row.original && (
              <ViewAllHostsLink
                queryParams={{
                  software_version_id: cellProps.row.original.id,
                }}
                responsive
                rowHover
              />
            )}
          </>
        );
      },
    },
  ];

  if (!isPremiumTier) {
    return configs.filter(
      (header) => header.accessor !== "resolved_in_version"
    );
  }

  return configs;
};

export default generateColumnConfigs;
